package com.kfm.dao;

import com.mysql.cj.jdbc.Driver;

import java.sql.*;
import java.util.Random;

public class JDBCDemo {


    public static void main(String[] args) {
        try {
            new JDBCDemo().test();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    public void test() throws SQLException {
        // 1. 注册驱动
        // DriverManager.registerDriver(new Driver());
        try {
            //
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        // 2. 获取连接
        // 数据库连接字符串 用户名 密码
        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/kfm", "root", "");
             // 3. 获取执行sql的对象
             Statement statement = conn.createStatement();) {
            // 4. 编写sql
            String sql = "select * from student";
            // 5. 执行sql
            ResultSet set = statement.executeQuery(sql);
            // 6. 解析结果
            while (set.next()) {
                System.out.println(set.getObject("id"));
                System.out.println(set.getObject("name"));
            }
        }

    }
}
